package com.team11.server.util;

public class PasswordValidate {
    public static String validatePassword(String password) {
        // type == 1 不带验证的密码
        // type == 2 非纯数字的密码
        if (password.length() < 6 || password.length() > 16) {
            return "密码为6~16字符";
        }
        /*
         * else if(doExecute(password) < 2){ return "用户密码为6~16字符"; }
         */
        else {
            if (doExecute(password) >= 2) {
                return "1";
            } else
                return "密码为6~16字符,包含字母、数字或特殊字符!";
        }
    }

    public static int doExecute(String password) {
        int kindOfCharacter = 0;
        String digital = "[0-9]";
        String capital = "[A-Z]";
        String lowercase = "[a-z]";
        String spec = "[-~!@#$%^&*()_+{}|:<>?;',.]";

        // 有大写字母或小写字母，即有字母
        for (int i = 0; i < password.length(); i++) {
            if (password.substring(i, i + 1).matches(capital)
                    || password.substring(i, i + 1).matches(lowercase)) {
                System.out.println(password.charAt(i));
                kindOfCharacter++;
                break;
            }
        }

        // 有数字
        for (int i = 0; i < password.length(); i++) {
            if (password.substring(i, i + 1).matches(digital)) {
                kindOfCharacter++;
                break;
            }
        }

        // 有符号
        for (int i = 0; i < password.length(); i++) {
            if (password.substring(i, i + 1).matches(spec)) {
                kindOfCharacter++;
                break;
            }
        }

        if (password.length() < 6) {
            kindOfCharacter = 1;
        }

        return kindOfCharacter;
    }
}
